package pers.ervinse.shoppingmall.user.fragment;

import static android.app.Activity.RESULT_OK;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;

import com.bumptech.glide.Glide;  // 引入 Glide 库

import pers.ervinse.shoppingmall.BaseFragment;
import pers.ervinse.shoppingmall.LoginActivity;
import pers.ervinse.shoppingmall.R;

public class UserFragment extends BaseFragment {

    private static final String TAG = UserFragment.class.getSimpleName();
    private static final int LOGIN_REQUEST_CODE = 1;

    // 当前登录状态
    private boolean isLogin = false;

    private ImageView user_photo_image;
    private TextView user_name_tv, user_desc_tv;
    private Button user_logout_btn;
    private View user_bar;

    /**
     * 初始化视图
     * @return
     */
    @Override
    public View initView() {
        Log.i(TAG, "用户视图初始化");
        View view = View.inflate(mContext, R.layout.fragment_user, null);
        user_photo_image = view.findViewById(R.id.user_photo_image);
        user_name_tv = view.findViewById(R.id.user_name_tv);
        user_desc_tv = view.findViewById(R.id.user_desc_tv);
        user_logout_btn = view.findViewById(R.id.user_logout_btn);
        user_bar = view.findViewById(R.id.user_bar);

        return view;
    }

    // 初始化数据
    public void initData() {
        super.initData();
        Log.i(TAG, "用户数据初始化");

        initListener();
    }

    /**
     * 初始化监听器
     */
    private void initListener() {
        // 登录
        user_bar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG, "用户登录事件");
                if (!isLogin) {
                    Log.i(TAG, "用户未登录");
                    // 前往登录页面
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    startActivityForResult(intent, LOGIN_REQUEST_CODE);
                }
            }
        });

        // 登出
        user_logout_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG, "用户登出事件");

                AlertDialog.Builder builder = new AlertDialog.Builder(mContext)
                        .setTitle("退出登录")
                        .setMessage("您是否要退出登录?")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                // 关闭对话框
                                dialogInterface.dismiss();
                                // 用户名恢复, 简介不可见
                                isLogin = false;
                                user_name_tv.setText("请登录");
                                user_desc_tv.setVisibility(View.GONE);

                                // 设置默认头像
                                Glide.with(mContext)
                                        .load(R.drawable.one)  // 默认头像
                                        .circleCrop()  // 圆形裁剪
                                        .into(user_photo_image);
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                // 关闭对话框
                                dialogInterface.dismiss();
                            }
                        });
                // 创建对话框, 并显示
                AlertDialog logoutDialog = builder.create();
                logoutDialog.show();
            }
        });
    }

    /**
     * 数据回传
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case LOGIN_REQUEST_CODE:
                if (resultCode == RESULT_OK && data != null) {
                    isLogin = true;
                    // 获取数据
                    String userName = data.getStringExtra("userName");
                    String avatarUrl = data.getStringExtra("avatarUrl");
                    int sex = data.getIntExtra("sex", 0);

                    Log.i(TAG, "用户登录数据回传: userName = " + userName
                            + ", avatarUrl = " + avatarUrl
                            + ", sex = " + sex);

                    // 设置用户名和头像
                    user_name_tv.setText(userName);

                    // 使用 Glide 加载头像
                    Glide.with(mContext)
                            .load(avatarUrl)  // 加载传递过来的 avatarUrl
                            .circleCrop()  // 圆形裁剪
                            .into(user_photo_image);

                    user_desc_tv.setVisibility(View.VISIBLE);
                    user_desc_tv.setText(sex == 1 ? "男" : "女");  // 假设 1 为男，0 为女
                }
                break;
            default:
        }
    }

    @Override
    public void refreshData() {
    }

    @Override
    public void saveData() {
    }
}
